package untils

import (
	"strings"
	"unicode/utf8"
)

// 简易版关键词提取（不依赖gojieba）
func ExtractKeywords(context string) []string {
	if context == "" {
		return []string{}
	}

	context = strings.TrimSpace(context)
	if context == "" {
		return []string{}
	}

	// 简单的空格和标点分割
	words := strings.FieldsFunc(context, func(r rune) bool {
		return isPunctuation(r) || r == ' ' || r == '　'
	})

	var keywords []string
	seen := make(map[string]bool)

	for _, word := range words {
		word = strings.TrimSpace(word)
		if isValidWord(word) && !isStopWord(word) {
			if !seen[word] {
				keywords = append(keywords, word)
				seen[word] = true
			}
		}
	}

	return keywords
}

// 判断是否为标点符号
func isPunctuation(r rune) bool {
	punctuations := "，。！？；：“”‘'（）【】《》〈〉、…—~`!@#$%^&*()_+-=[]{}|;:',.<>?/"
	return strings.ContainsRune(punctuations, r)
}

// 检查词语有效性
func isValidWord(word string) bool {
	if utf8.RuneCountInString(word) <= 1 {
		return false
	}

	// 简单的长度和字符检查
	for _, r := range word {
		if !isChineseChar(r) && !isLetterOrDigit(r) {
			return false
		}
	}
	return true
}

// 中文字符判断
func isChineseChar(r rune) bool {
	return r >= 0x4E00 && r <= 0x9FFF
}

// 字母数字判断
func isLetterOrDigit(r rune) bool {
	return (r >= 'a' && r <= 'z') || (r >= 'A' && r <= 'Z') || (r >= '0' && r <= '9')
}

// 停用词过滤
func isStopWord(word string) bool {
	stopWords := map[string]bool{
		"的": true, "了": true, "在": true, "是": true, "我": true,
		"有": true, "和": true, "就": true, "不": true, "人": true,
		"都": true, "一": true, "一个": true, "上": true, "也": true,
		"很": true, "到": true, "说": true, "要": true, "去": true,
		"你": true, "他": true, "她": true, "它": true, "我们": true,
		"你们": true, "他们": true, "这个": true, "那个": true,
		"啊": true, "呀": true, "呢": true, "吧": true, "嘛": true,
	}
	return stopWords[word]
}
             